console.log("background js");

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(null, {file: "jquery-3.6.0.min.js"});
    chrome.tabs.executeScript(null, {file: "content_script.js"});
    chrome.tabs.executeScript(null, {file: "sdcl.js"});
    chrome.tabs.executeScript(null, {file: "code_totp.js"});
});

chrome.runtime.onMessage.addListener(function (request,sender,callback) {
    console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");

    // 处理host添加
    if (request.hoststr){
        $.ajax({
            url:'http://127.0.0.1:50761/api/addList?id=1&context='+request.hoststr,
            type:'get',
            async:false,
            contentType: 'text',
            success:function(res){
                console.log(res);
                callback({resp: res});
            },
            error: function(e){
                console.log(e);
                callback({resp: 'error'});
            }
        });
    }

    // 处理心跳右键菜单添加
    if('heartbeat' == request.menu){
        console.log('添加右键心跳菜单');
        chrome.contextMenus.create({
            id:"heartbeat",
            type : "normal",
            title : "切换心跳开关",
            contexts: ['all'],
            documentUrlPatterns: ['https://dev.open.10086.cn/web/cmd/*','http://dev.open.hq.cmcc/*']
            },
            function () {
                console.log('contextMenus are create.');
                callback({resp: 'success create'});
            }
        );
    }
    if('heartbeatRemove' == request.menu){
        chrome.contextMenus.remove('heartbeat');
        callback({resp: 'success create'});
    }

    if('smsCode'==request.menu){
        chrome.notifications.create("smsInfo", {	
            type : 'basic',
            title : '验证码',
            message : request.code,
            iconUrl : 'ico16.png',
            requireInteraction: false
        });
    }

    if(request.sql){
        console.log('sql request ' + request.sql);
        $.ajax({
            url:'https://sandbox.open.10086.cn/67776666/externalchannel/check//test/sql/test',
            type:'POST',
            data: request.sql,
            async:false,
            dataType:'json',
            contentType: 'text/plain',
            success:function(res){
                console.log(res);
                callback({resp: res});
            },
            error: function(e){
                console.log(e);
                callback({resp: 'error'});
            }
        });
    }
});

chrome.contextMenus.onClicked.addListener(function(info, tab) {
    if('heartbeat' == info.menuItemId){
        console.log('click menu heartbeat');
        chrome.tabs.sendMessage(tab.id, {'type': 'switchHeartBeat', 'info': info});
    }else if('newTab' == info.menuItemId){
        console.log('click menu newTab');
        chrome.tabs.sendMessage(tab.id, {'type': 'newTab', 'info': info});
    }else if('newSqlTab' == info.menuItemId){
        console.log('开启沙箱SQL查询页面');
        chrome.tabs.sendMessage(tab.id, {'type': 'newSqlTab', 'info': info});
        chrome.tabs.create({url: info.pageUrl});
    }else if('newTerminalsTab' == info.menuItemId){
        console.log('开启终端查询页面');
        chrome.tabs.sendMessage(tab.id, {'type': 'newTerminalsTab', 'info': info});
        chrome.tabs.create({ url: chrome.runtime.getURL('./html/shareTerminal/index.html') });
    }
});

chrome.contextMenus.create({
    id:"newTab",
    type : "normal",
    title : "新开页面",
    contexts: ['all'],
    documentUrlPatterns: ['https://dev.open.10086.cn/*','http://dev.open.hq.cmcc/*']
    },
    function () {
        console.log('contextMenus newTab are create.');
    }
);

chrome.contextMenus.create({
    id:"newSqlTab",
    type : "normal",
    title : "沙箱数据库查询",
    contexts: ['all'],
    documentUrlPatterns: ['https://dev.open.10086.cn/*','http://dev.open.hq.cmcc/*']
    },
    function () {
        console.log('contextMenus newSqlTab are create.');
    }
);

chrome.contextMenus.create({
    id:"newTerminalsTab",
    type : "normal",
    title : "终端列表",
    contexts: ['all'],
    documentUrlPatterns: ['https://dev.open.10086.cn/*','http://dev.open.hq.cmcc/*']
    },
    function () {
        console.log('contextMenus newTerminalsTab are create.');
    }
);